home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / GLUT-3.7 / PROGS / DEMOS / rollercoaster / DEFRC.C next >
Encoding:
C/C++ Source or Header  |  1998-08-12  |  6.5 KB  |  306 lines

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <math.h>
  4. #include <GL/glut.h>
  5. #include "matrix.h"
  6.  
  7. /* Some <math.h> files do not define M_PI... */
  8. #ifndef M_PI
  9. #define M_PI 3.14159265358979323846
  10. #endif
  11.  
  12. typedef struct parameter
  13. {
  14.     double value;
  15.     double speed;
  16.     int steps;
  17. } Parameter;
  18.  
  19. Matrix pos;
  20. Parameter roll, alignment, heading, pitch;
  21. double tot_al = 0.0;
  22. int tot = 0;
  23.  
  24. #define MAX 10000
  25.  
  26. extern GLfloat x[MAX], y[MAX], z[MAX];
  27. extern GLfloat dx[MAX], dy[MAX], dz[MAX];
  28. extern GLfloat hd[MAX], al[MAX], pt[MAX], rl[MAX];
  29. extern GLfloat strips[27][MAX][3], normal[24][MAX][3];
  30. extern int opt[MAX];
  31. extern GLfloat bnormal[2][MAX][3];
  32. extern GLfloat r1[MAX], r2[MAX], r3[MAX];
  33.  
  34. void update_parameters(void);
  35. void update_parameter(Parameter *p);
  36. void init_parameter(Parameter *p);
  37.  
  38. Vector strips_tmp[27], normal_tmp[27];
  39.  
  40. void calculate_rc(void)
  41. {
  42.     FILE *in;
  43.     int i, j;
  44.     char cmd[256];
  45.     double a;
  46.     Vector v;
  47.     Matrix tmp, t2;
  48. #if 0
  49.     GLfloat nx, ny, nz, ac;
  50. #endif
  51.  
  52.     printf("Reading RC parameters.\n");
  53. /*
  54.     out = fopen("rc.in", "w");
  55.     if (!out)
  56.     {
  57.     fprintf(stderr, "Failed to open file 'rc.in' for writing.\n");
  58.     exit(1);
  59.     }
  60. */
  61.  
  62.     in = fopen("rc.def", "r");
  63.     if (!in)
  64.     {
  65.     fprintf(stderr, "Failed to open file 'rc.def'.\n");
  66.     exit(1);
  67.     }
  68.  
  69.     init_parameter(&roll);
  70.     init_parameter(&pitch);
  71.     init_parameter(&heading);
  72.     init_parameter(&alignment);
  73.  
  74.     init_matrix(&pos);
  75.     pos.index[3][2] = 0;
  76.  
  77.     for (i=0;i<27;i++)
  78.     {
  79.     init_vector(&normal_tmp[i]);
  80.     init_vector(&strips_tmp[i]);
  81.     }
  82.  
  83.     for (i=0;i<8;i++)
  84.     {
  85.     strips_tmp[i].index[2] = 1.0;
  86.     strips_tmp[i+8].index[2] = -1.0;
  87.     strips_tmp[i+16].index[1] = -1.0;
  88.     }
  89.  
  90.     for (i=0;i<3;i++)
  91.     {
  92.     for (j=0;j<8;j++)
  93.     {
  94.         normal_tmp[i*8+j].index[2] = cos(j*M_PI/4);
  95.         normal_tmp[i*8+j].index[1] = sin(j*M_PI/4);
  96.         strips_tmp[i*8+j].index[2] += cos(j*M_PI/4)/(i==2?2:4);
  97.         strips_tmp[i*8+j].index[1] += sin(j*M_PI/4)/(i==2?2:4);
  98.     }
  99.     strips_tmp[24].index[2] = 1.0;
  100.     strips_tmp[25].index[2] = -1.0;
  101.     strips_tmp[26].index[1] = -1.0;
  102.     }
  103.  
  104.     while (!feof(in))
  105.     {
  106.     for (i=0;i<256;i++)
  107.     {
  108.         int ch;
  109.  
  110.         ch = fgetc(in);
  111.         if ((ch == '\n') || (ch == EOF))
  112.         {
  113.         cmd[i] = 0;
  114.         break;
  115.         }
  116.         cmd[i] = ch;
  117.     }
  118.     if (cmd[0] == '#')
  119.         continue;
  120.     else if (!cmd[0])
  121.         continue;
  122.     else if (sscanf(cmd, "pitch %lf %d", &a, &i))
  123.     {
  124.         pitch.speed = (a - pitch.value)/i;
  125.         pitch.steps = i;
  126.     }
  127.     else if (sscanf(cmd, "alignment %lf %d", &a, &i))
  128.     {
  129.         alignment.speed = (a - alignment.value)/i;
  130.         alignment.steps = i;
  131.     }
  132.     else if (sscanf(cmd, "heading %lf %d", &a, &i))
  133.     {
  134.         heading.speed = (a - heading.value)/i;
  135.         heading.steps = i;
  136.     }
  137.     else if (sscanf(cmd, "roll %lf %d", &a, &i))
  138.     {
  139.         roll.speed = (a - roll.value)/i;
  140.         roll.steps = i;
  141.     }
  142.     else if (sscanf(cmd, "wait %d", &i))
  143.     {
  144.         for (;i>=0;i--)
  145.         {
  146.         update_parameters();
  147.  
  148.         init_vector(&v);
  149.         v.index[0] = 0.15;
  150.         multiply_matrix_vector(&pos, &v);
  151.         for (j=0;j<3;j++)
  152.             pos.index[3][j] = v.index[j];
  153.  
  154.         rotate_x(-roll.value*M_PI/(180*50), &pos);
  155.         rotate_y(-heading.value*M_PI/(180*50), &pos);
  156.         rotate_z(-pitch.value*M_PI/(180*50), &pos);
  157.  
  158.         x[tot] = v.index[0];
  159.         y[tot] = v.index[1];
  160.         z[tot] = v.index[2];
  161.         al[tot] = alignment.value/50.0;
  162.         rl[tot] = roll.value/50.0;
  163.         hd[tot] = heading.value/50.0;
  164.         pt[tot] = pitch.value/50.0;
  165.         opt[tot] = 100*fabs(rl[tot] - al[tot]) + 100*fabs(hd[tot])+
  166.             100*fabs(pt[tot]);
  167.  
  168.         copy_matrix(&tmp, &pos);
  169.         init_vector(&v);
  170.         v.index[1] = 1;
  171.         tot_al += alignment.value*M_PI/180/50;
  172.         rotate_x(-tot_al, &tmp);
  173.         multiply_matrix_vector(&tmp, &v);
  174.         dx[tot] = v.index[0] - tmp.index[3][0];
  175.         dy[tot] = v.index[1] - tmp.index[3][1];
  176.         dz[tot] = v.index[2] - tmp.index[3][2];
  177.  
  178.         copy_matrix(&tmp, &pos);
  179.         tot_al += alignment.value*M_PI/180/50;
  180.         rotate_x(-tot_al, &tmp);
  181.         copy_matrix(&t2, &tmp);
  182.         for (j=0;j<27;j++)
  183.         {
  184.             copy_vector(&v, &strips_tmp[j]);
  185.             multiply_matrix_vector(&t2, &v);
  186.             
  187.             strips[j][tot][0] = v.index[0];
  188.             strips[j][tot][1] = v.index[1];
  189.             strips[j][tot][2] = v.index[2];
  190.  
  191.             copy_vector(&v, &normal_tmp[j]);
  192.             multiply_matrix_vector(&t2, &v);
  193.             
  194.             normal[j][tot][0] = v.index[0] - t2.index[3][0];
  195.             normal[j][tot][1] = v.index[1] - t2.index[3][1];
  196.             normal[j][tot][2] = v.index[2] - t2.index[3][2];
  197.         }
  198.         init_vector(&v);
  199.         v.index[0] = -1.0;
  200.         v.index[1] = -1.5;
  201.         multiply_matrix_vector(&pos, &v);
  202.         bnormal[0][tot][0] = v.index[0] - pos.index[3][0];
  203.         bnormal[0][tot][1] = v.index[1] - pos.index[3][1];
  204.         bnormal[0][tot][2] = v.index[2] - pos.index[3][2];
  205.  
  206.         init_vector(&v);
  207.         v.index[2] = -1.0;
  208.         v.index[1] = 1.5;
  209.         multiply_matrix_vector(&pos, &v);
  210.         bnormal[0][tot][0] = v.index[0] - pos.index[3][0];
  211.         bnormal[0][tot][1] = v.index[1] - pos.index[3][1];
  212.         bnormal[0][tot][2] = v.index[2] - pos.index[3][2];
  213.  
  214. #if 0
  215.         copy_matrix(&tmp, &pos);
  216.         tmp.index[3][0] = 0.0;
  217.         tmp.index[3][1] = 0.0;
  218.         tmp.index[3][2] = 0.0;
  219.  
  220.         init_vector(&v);
  221.         v.index[0] = 1.0;
  222.         multiply_matrix_vector(&tmp, &v);
  223.  
  224.         nx = v.index[0];
  225.         ny = v.index[1];
  226.         nz = v.index[2];
  227.  
  228.         ac = sqrt(nx*nx+nz*nz);
  229.  
  230.         if (ac == 0.0)
  231.             r1[tot] = 0.0;
  232.         else if (nx > 0)
  233.             r1[tot] = asin(nz/ac);
  234.         else
  235.             r1[tot] = M_PI-asin(nz/ac);
  236.  
  237.         r2[tot] = asin(ny);
  238.  
  239.         rotate_y(-r1[tot], &tmp);
  240.         rotate_z(-r2[tot], &tmp);
  241.         
  242.         init_vector(&v);
  243.         v.index[1] = 1.0;
  244.         multiply_matrix_vector(&tmp, &v);
  245.         nx = v.index[0];
  246.         ny = v.index[1];
  247.         nz = v.index[2];
  248.  
  249.         ac = sqrt(nz*nz+ny*ny); /* this *should* be 1 */
  250.  
  251.         if (ac == 0.0)
  252.             r3[tot] = 0.0;
  253.         else if (nz > 0)
  254.             r3[tot] = M_PI-asin(ny/ac);
  255.         else
  256.             r3[tot] = asin(ny/ac);
  257. #endif
  258.  
  259.         copy_matrix(&tmp, &pos);
  260.         rotate_x(-tot_al, &tmp);
  261.         if (tmp.index[0][0] == 0.0 && tmp.index[0][1] == 0.0) {
  262.             r1[tot] = atan2(- tmp.index[1][0], - tmp.index[2][0]);
  263.             r2[tot] = 0.5 * M_PI;
  264.             r3[tot] = 0.0;
  265.         } else {
  266.             r1[tot] = atan2(tmp.index[1][2], tmp.index[2][2]);
  267.             r2[tot] = asin(tmp.index[0][2]);
  268.             r3[tot] = atan2(tmp.index[0][1], tmp.index[0][0]);
  269.         }
  270.  
  271. #if 0
  272.         printf("R: %f, %f, %f.\n", r1[tot], r2[tot], r3[tot]);
  273. #endif
  274.         tot ++;
  275.         }
  276.     }
  277.     else
  278.        fprintf(stderr, "Not understood: %s\n", cmd);
  279.     }
  280.     printf("Done.\nTotal of %d parts\n", tot);
  281.     printf("Ended at %f, %f, %f\n", x[tot-1], y[tot-1], z[tot-1]);
  282. }
  283.  
  284. void update_parameters(void)
  285. {
  286.     update_parameter(&roll);
  287.     update_parameter(&pitch);
  288.     update_parameter(&heading);
  289.     update_parameter(&alignment);
  290. }
  291.  
  292. void update_parameter(Parameter *p)
  293. {
  294.     if (!p->steps)
  295.     return;
  296.     p->steps--;
  297.     p->value += p->speed;
  298. }
  299.  
  300. void init_parameter(Parameter *p)
  301. {
  302.     p->value = 0;
  303.     p->speed = 0;
  304.     p->steps = 10;
  305. }
  306.